/**
 * 实现compose
 */

// const { flowRight } = require('lodash')

// const reverse = arr => arr.reverse()
// const first = arr => arr[0]
// const toUpper = str => str.toUpperCase()

// const f = flowRight(toUpper, first, reverse)
// console.log(f(['hello fp', 'hello compose', 'hello flowRight']))

/**
 * 分析
 * compose接收不固定数量的函数，返回一个新函数
 * 新函数接收一个参数，将参数用compose接收到的函数从右到左处理
 * 新函数返回处理后的数据
 */

const compose =  (...fns) => value =>
  fns.reverse().reduce((value, fn) => fn(value), value)

const reverse = <T>(arr: T[]) => arr.reverse()
const first = <T>(arr: T[]) => arr[0]
const toUpper = (str: string) => str.toUpperCase()

const f = compose(toUpper, first, reverse)
console.log(f(['hello fp', 'hello compose', 'hello flowRight']))

export {}

